package com.cskaoyan.service.topic.admin;

import com.cskaoyan.bean.ListData;
import com.cskaoyan.bean.Topic;
import com.cskaoyan.bean.TopicExample;
import com.cskaoyan.bean.topic.CreateTopicBO;
import com.cskaoyan.bean.topic.UpdateTopicBO;
import com.cskaoyan.mapper.TopicMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
@Service
public class TopicServiceImpl implements TopicService {

    @Autowired
    TopicMapper topicMapper;


    @Override
    public ListData<Topic> list(String page, String limit, String title, String subtitle, String sort, String order) {

        PageHelper.startPage(Integer.valueOf(page),Integer.valueOf(limit));
        TopicExample topicExample = new TopicExample();
        topicExample.setOrderByClause(sort + " " + order);
        TopicExample.Criteria criteria = topicExample.createCriteria();
        if (title != null){
            criteria.andTitleLike("%"+title+"%");
        }
        if (subtitle != null){
            criteria.andSubtitleLike("%"+subtitle+"%");
        }
//用哪个方法？
        List<Topic> list = topicMapper.selectByExampleWithBLOBs(topicExample);
        ListData<Topic> listData = new ListData<>();
        listData.setItems(list);

        PageInfo<Topic> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        listData.setTotal((int) total);

        return listData;
    }

    @Override
    public boolean delete(int id) {
        int i = topicMapper.deleteByPrimaryKey(id);
        return 1 == i;
    }

    @Override
    public boolean create(CreateTopicBO createTopicBO) {
        Topic topic = new Topic();
        topic.setSubtitle(createTopicBO.getSubtitle());
        topic.setPicUrl(createTopicBO.getPicUrl());
        topic.setContent(createTopicBO.getContent());
        topic.setPrice(new BigDecimal(createTopicBO.getPrice()));
        topic.setReadCount(createTopicBO.getReadCount());
        topic.setTitle(createTopicBO.getTitle());
        //
        topic.setGoods("[]");
        int i = topicMapper.insertSelective(topic);
        return 1 == i;
    }

    @Override
    public boolean update(UpdateTopicBO updateTopicBO) {
        Topic topic = new Topic();
        topic.setId(updateTopicBO.getId());
        topic.setTitle(updateTopicBO.getTitle());
        topic.setSubtitle(updateTopicBO.getSubtitle());
        topic.setPrice(updateTopicBO.getPrice());
        topic.setReadCount(updateTopicBO.getReadCount());
        topic.setPicUrl(updateTopicBO.getPicUrl());
        topic.setAddTime(updateTopicBO.getAddTime());
        topic.setUpdateTime(updateTopicBO.getUpdateTime());
        topic.setContent(updateTopicBO.getContent());

//        String goods = JsonTool.parseListToString(updateTopicBO.getGoods());
        //
        topic.setGoods((String) updateTopicBO.getGoods());
        int i = topicMapper.updateByPrimaryKeySelective(topic);
        return 1 == i;
    }

    @Override
    public int selectMaxId() {
        return topicMapper.selectMaxId();
    }

    @Override
    public Topic selectByPrimaryKey(int i) {
        Topic topic = topicMapper.selectByPrimaryKey(i);
        return topic;
    }
}
